<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午3:48
 */
class SupplyModel extends BaseModel{
    private $_table;
    protected $_name = 'users_invoice';
    protected $_isbug = false;
    public function __construct() {
        parent::__construct();
    }

    public function getSupplyListByWhere($where,$item_page,$offset,$order,$group,$having){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,array("id","business_name","contacts","mobile","business_phone","main_variety","visit") )
            ->joinLeft ("iq_quotation","$this->_name.id = iq_quotation.business_id","max(iq_quotation.update_time) as update_time")
            ->joinLeft("product","product.id = iq_quotation.product_id","product_name")
            ->joinLeft("product_cates","product_cates.id = product.cate_id","cate_name")
            ->joinLeft("product_brands","product_brands.id = product.brand_id","brand_name")
            ->joinLeft("region as r","r.region_id = $this->_name.business_city_id","region_name")
            ->joinLeft("region","region.region_id = iq_quotation.area_id","region_name as area_name")
            ->where ( $where )->__toString ();
        if($group){
            $sql.= " group  by ".$group;
        }

        if($having){
            $sql.=" HAVING {$having}";
        }

        if($order){
            $sql.= " order  by ".$order;
        }

        if($item_page){
            $sql.= " limit $offset,$item_page";
        }



        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }

        $data = $db->fetchAll ( $sql );
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['update_time'] = Tools::time2Units(time() - strtotime($v['update_time'])) . "前";
            }
        }

        return $data;
    }

    public function getSupplyTatolWhere($where ="1=1",$group,$having){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }

        $sql = $db->select ()->from ( $this->_name ,"count(*)")
            ->joinLeft ("iq_quotation","$this->_name.id = iq_quotation.business_id","")
            ->joinLeft("product","product.id = iq_quotation.product_id","")
            ->joinLeft("product_cates","product_cates.id = product.cate_id","")
            ->joinLeft("product_brands","product_brands.id = product.brand_id","")
            ->joinLeft("region as r","r.region_id = $this->_name.business_city_id","region_name")
            ->joinLeft("region","region.region_id = iq_quotation.area_id","region_name as area_name")
            ->where ( $where )->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }

        if($having){
            $sql.=" HAVING {$having}";
        }
        $sql = "select count(*) from(".$sql.")  as total";
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }

        return $db->fetchOne( $sql );
    }
    ##########################################
    /**
     * @param string $where
     * @param $limit
     * @return array
     */
    public function getSupplyByWhere($where ="1=1",$limit){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from("users_invoice as ui",array("business_name","contacts","mobile","main_variety"))
            ->joinLeft ("iq_quotation as iq","ui.id = iq.business_id","max(iq.update_time) as update_time")
            ->where($where)
            ->order("max(iq.update_time) DESC")
            ->group("iq.business_id")
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        if(!empty($limit)){
            $sql.=" limit {$limit}";
        }
        return $db->fetchAll ( $sql );
    }

    /**
     * @param string $where
     * @param $order
     * @param $limit
     * @return array
     */
    public function getRankingByWhere($where ="1=1",$order,$limit){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from("users_invoice as ui",array("business_name","visit","id"))
            ->where($where)
            ->order($order)
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        if(!empty($limit)){
            $sql.=" limit {$limit}";
        }
        return $db->fetchAll ( $sql );
    }


}